$(document).ready(function () {

    $("#shifts").kendoComboBox({
        dataTextField: "text",
        dataValueField: "value",
        dataSource: appBosch.shifts,
        filter: "contains",
        suggest: true,
        placeholder: "Seleccionar turno..."
    });

    //Generamos la ventana para la edición de los grupos
    appBosch.wndTeam = $('#wndTeam');
    appBosch.wndTeam.kendoWindow({
        visible: false,
        width: 1000,
        height: 600,
        open: function () {
            /// Cargamos las lineas asignadas al equipo
            var urlTeamLines = [appBosch.urlTeamLinesRead, appBosch.selectedTeam.id].join('/');
            loadTeamLines(urlTeamLines);
            var urlTeamUsers = [appBosch.urlActiveTeamUsers, appBosch.selectedTeam.id].join('/');
            loadTeamUsers(urlTeamUsers);
            $("#shifts").data('kendoComboBox').value(appBosch.selectedTeam.shiftId);
            this.center();
        },
        close: function () {
            $('#wndTeam').find('.container-users input:first-of-type').val('');
            $('#wndTeam').find('.container-users .col-lg-5 input:first-of-type').val('');
            var id = appBosch.selectedTeam.id;
            var name = $('#nameTeam').val();
            var urlSaveNameTeam = [appBosch.urlSaveNameTeam, id].join('/');
            var view = $(appBosch.selectedTeam.view);
            view.find('i.fa-refresh').removeClass('hidden');
            view.find('i.fa-group').addClass('hidden');
            var shift = $("#shifts").data('kendoComboBox').value();
            $.getJSON(urlSaveNameTeam,
                {
                    name: name,
                    shift: shift
                }, function (team) {
                    var code = appBosch.templateTeam(team);
                    view.replaceWith(code);
                });
        }
    });
    appBosch.wndTeam = appBosch.wndTeam.data('kendoWindow');
    /// Cargamos los templates
    appBosch.templateTeam = kendo.template($('#templateTeam').html());
    appBosch.templateGroupDetail = kendo.template($('#templateGroupDetail').html());
    /// Agregamos los grupos al contenedor
    loadTeams();

    $("#formGetTeamByUser input").keyup(function (e) {
        if (e.keyCode === 13) {
            seekInTeams();
        }
    });

    $('#formGetTeamByUser button').click(seekInTeams);

    $('#formGetTeamByUser input').change(function () {
        var seed = $(this).val().trim();
        if (seed === '') {
            $('.container-teams').removeClass('hidden');
            $('.container-teams-filter').addClass('hidden');
        }
    });

    $('.container-teams-filter button').click(function () {
        $('#formGetTeamByUser input').val('');
        $('#formGetTeamByUser input').trigger('change');
        var spin = $('#formGetTeamByUser i.fa-refresh');
        var button = $('#formGetTeamByUser button');
        spin.addClass('hidden');
        button.removeClass('hidden');
    });

    $("#exportPDF").click(function () {
        var teams = $('.container-teams');
        if (!$('.container-teams-filter').is(":hidden")) { //por si hay equipos filtrados
            teams = $('.container-teams-filter .results');
        }
        kendo.drawing.drawDOM(teams)
            .then(function (group) {
                kendo.drawing.pdf.saveAs(group, "Equipos.pdf");
            });
    });
});


function seekInTeams() {
    var seed = $('#formGetTeamByUser input').val().trim();
    var spin = $('#formGetTeamByUser i.fa-refresh');
    var button = $('#formGetTeamByUser button');
    if (seed !== '') {
        spin.removeClass('hidden');
        button.addClass('hidden');

        var areasId = [];
        for (var i = 0; i < appBosch.areas.length; i++) {
            areasId.push(appBosch.areas[i].area_id);
        }//for areas
        $.getJSON(appBosch.urlGetTeamBySeed + '/0', {
            areas: areasId,
            seed: seed
        }, function (teams) {
            $('.container-teams').addClass('hidden');
            $('.container-teams-filter').removeClass('hidden');
            if (teams.length > 0) {
                $('.container-teams div.noResults').addClass('hidden');
                $('.container-teams-filter div.results').removeClass('hidden');
                var containerTeams = $('.container-teams-filter div.teams');
                containerTeams.html('');
                teams.forEach(function (team) {
                    var code = appBosch.templateTeam(team);
                    containerTeams.append(code);
                    containerTeams.find('div.team').last().kendoDropTarget({
                        dragenter: droptargetOnDragEnter,
                        dragleave: droptargetOnDragLeave,
                        drop: droptargetOnDrop
                    });
                });
            }
            else {
                $('.container-teams-filter div.teams').html('');
                $('.container-teams-filter div.noResults').removeClass('hidden');
                $('.container-teams-filter div.results').addClass('hidden');
            }
            spin.addClass('hidden');
            button.removeClass('hidden');
        });
    }
}